package com.zyj.repairs.service.impl;

import java.util.List;

import com.zyj.assets.domain.AssetsInfo;
import com.zyj.assets.mapper.AssetsInfoMapper;
import com.zyj.common.core.domain.entity.SysRole;
import com.zyj.common.core.domain.entity.SysUser;
import com.zyj.common.core.text.Convert;
import com.zyj.common.utils.DateUtils;
import com.zyj.common.utils.http.HttpUtils;
import com.zyj.persion.mapper.AssetsPresionMapper;
import com.zyj.receive.domain.AssetsReceive;
import com.zyj.receive.mapper.AssetsReceiveMapper;
import com.zyj.system.service.ISysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zyj.repairs.mapper.AssetsRepairsMapper;
import com.zyj.repairs.domain.AssetsRepairs;
import com.zyj.repairs.service.IAssetsRepairsService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 资产维修Service业务层处理
 * 
 * @author zhuyujie
 * @date 2022-05-12
 */
@Service
public class AssetsRepairsServiceImpl implements IAssetsRepairsService 
{
    @Autowired
    private AssetsRepairsMapper assetsRepairsMapper;

    @Autowired
    private AssetsInfoMapper assetsInfoMapper;

    @Autowired
    private AssetsReceiveMapper assetsReceiveMapper;

    @Autowired
    private AssetsPresionMapper assetsPresionMapper;

    @Autowired
    private ISysRoleService roleService;

    /**
     * 查询资产维修
     * 
     * @param repairsId 资产维修主键
     * @return 资产维修
     */
    @Override
    public AssetsRepairs selectAssetsRepairsByRepairsId(Long repairsId)
    {
        return assetsRepairsMapper.selectAssetsRepairsByRepairsId(repairsId);
    }

    /**
     * 查询资产维修列表
     * 
     * @param assetsRepairs 资产维修
     * @return 资产维修
     */
    @Override
    public List<AssetsRepairs> selectAssetsRepairsList(AssetsRepairs assetsRepairs)
    {
        return assetsRepairsMapper.selectAssetsRepairsList(assetsRepairs);
    }

    /**
     * 新增资产维修
     * 
     * @param assetsRepairs 资产维修
     * @return 结果
     */
    @Override
    public int insertAssetsRepairs(AssetsRepairs assetsRepairs)
    {
        assetsRepairs.setCreateTime(DateUtils.getNowDate());
        //修改设备领用状态
        AssetsInfo infonew = new AssetsInfo();
        infonew.setId(assetsRepairs.getAssetsId());
        infonew.setAssetsStatus("3");
        assetsInfoMapper.updateAssetsInfo(infonew);
        int result = assetsRepairsMapper.insertAssetsRepairs(assetsRepairs);

        try{
            //通知资产管理员有维修任务
            //1根据角色编号获取所有角色人员
            String msg = "有一个资产维修工单需要你处理：" +
                    "资产编号："+assetsRepairs.getAssetsId()+"("+assetsRepairs.getAssetsName()+")， " +
                    "报修人："+assetsRepairs.getUserName()+"("+assetsRepairs.getUserMobile()+")，" +
                    "位置："+assetsRepairs.getUserSeat() ;
            String json = "{\"userids\":\"&userids&\",\"msg\":\""+msg+"\"}";
            List<SysUser> userlist =  roleService.selectUserListByRoleId(4L);
            if(userlist!=null && !userlist.isEmpty()){
                String userids = "";
                for(int i=0;i<userlist.size();i++){
                    SysUser user = userlist.get(i);
                    String externalUserid = user.getExternalUserid();
                    userids+=externalUserid+",";
                }
                if(!"".equals(userids)){
                    userids=userids.substring(0, userids.length()-1);
                    json = json.replace("&userids&", userids);
                    System.out.println("json:"+json);
                    HttpUtils.sendPost("http://localhost/feishu/sendMsg/", "json=" + json);
                }
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 修改资产维修
     * 
     * @param assetsRepairs 资产维修
     * @return 结果
     */
    @Transactional
    @Override
    public int updateAssetsRepairs(AssetsRepairs assetsRepairs)
    {
        if("2".equals(assetsRepairs.getRepairsStatus())){
            //报废
            //修改设备领用状态
            AssetsInfo infonew = new AssetsInfo();
            infonew.setId(assetsRepairs.getAssetsId());
            infonew.setAssetsStatus("4");
            assetsInfoMapper.updateAssetsInfo(infonew);
            //删除人员设备
            assetsPresionMapper.deleteAssetsPresionByAssetsId(assetsRepairs.getAssetsId());
            //记录报废
            AssetsReceive assetsReceive = new AssetsReceive();
            assetsReceive.setCreateTime(DateUtils.getNowDate());
            assetsReceive.setAssetsId(assetsRepairs.getAssetsId());
            assetsReceive.setAssetsName(assetsRepairs.getAssetsName());
            assetsReceive.setAssetsSn(assetsRepairs.getAssetsSn());
            assetsReceive.setUserId(assetsRepairs.getUserId());
            assetsReceive.setUserName(assetsRepairs.getUserName());
            assetsReceive.setReceiveTime(DateUtils.getNowDate());
            assetsReceive.setReceiveNo("SC" + DateUtils.dateTimeNow());
            assetsReceive.setReceiveType("4");
            assetsReceive.setReceiveDesc("报修后维修失败报废");
            assetsReceive.setCreateBy(assetsRepairs.getCreateBy());
            assetsReceiveMapper.insertAssetsReceive(assetsReceive);

        }else if("3".equals(assetsRepairs.getRepairsStatus())){
            //完结获取之前的领用状态
            AssetsReceive re = new AssetsReceive();
            re.setAssetsId(assetsRepairs.getAssetsId());
            re.setUserId(assetsRepairs.getUserId());
            List<AssetsReceive> list = assetsReceiveMapper.selectAssetsReceiveList(re);
            if (list != null && !list.isEmpty()) {
                AssetsReceive ar = list.get(0);
                //修改设备状态改回去
                String as = ar.getReceiveType();
                String assetsStatus = "";
                //领用或借出
                if ("1".equals(as) || "2".equals(as)) {
                    assetsStatus = as;
                }
                AssetsInfo infonew = new AssetsInfo();
                infonew.setId(assetsRepairs.getAssetsId());
                infonew.setAssetsStatus(assetsStatus);
                assetsInfoMapper.updateAssetsInfo(infonew);

            }
        }

        return assetsRepairsMapper.updateAssetsRepairs(assetsRepairs);
    }

    /**
     * 批量删除资产维修
     * 
     * @param repairsIds 需要删除的资产维修主键
     * @return 结果
     */
    @Override
    public int deleteAssetsRepairsByRepairsIds(String repairsIds)
    {
        return assetsRepairsMapper.deleteAssetsRepairsByRepairsIds(Convert.toStrArray(repairsIds));
    }

    /**
     * 删除资产维修信息
     * 
     * @param repairsId 资产维修主键
     * @return 结果
     */
    @Override
    public int deleteAssetsRepairsByRepairsId(Long repairsId)
    {
        return assetsRepairsMapper.deleteAssetsRepairsByRepairsId(repairsId);
    }
}
